<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  

  
  <title>golang-pprof性能分析工具 | Abel&#39;Blog</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="编写好了golang服务之后，接着要开始关注服务的CPU，内存使用情况。golang提供了性能剖析工具，记录一些自己搜集到的信息，写下一些实践的情况。在golang中内置了pprof工具，专门来做golang语言的优化。  1.安装环境 2.性能监控代码 3.CPU性能查看 内存泄漏 3.引用  1.安装环境1go get -u github.com&#x2F;google&#x2F;pprof  需要去下载一个绘图">
<meta property="og:type" content="article">
<meta property="og:title" content="golang-pprof性能分析工具">
<meta property="og:url" content="http://example.com/2021/07/28/2020/2020-11-14-golang-pprof/index.html">
<meta property="og:site_name" content="Abel&#39;Blog">
<meta property="og:description" content="编写好了golang服务之后，接着要开始关注服务的CPU，内存使用情况。golang提供了性能剖析工具，记录一些自己搜集到的信息，写下一些实践的情况。在golang中内置了pprof工具，专门来做golang语言的优化。  1.安装环境 2.性能监控代码 3.CPU性能查看 内存泄漏 3.引用  1.安装环境1go get -u github.com&#x2F;google&#x2F;pprof  需要去下载一个绘图">
<meta property="og:locale" content="en_US">
<meta property="og:image" content="https://s3.ax1x.com/2020/12/30/rL0XUx.png">
<meta property="og:image" content="https://s3.ax1x.com/2020/12/30/rLfQUg.png">
<meta property="og:image" content="https://s3.ax1x.com/2020/12/30/rL0OV1.png">
<meta property="article:published_time" content="2021-07-28T03:33:01.205Z">
<meta property="article:modified_time" content="2021-06-22T05:53:15.520Z">
<meta property="article:author" content="Abel Sean">
<meta property="article:tag" content="golang">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://s3.ax1x.com/2020/12/30/rL0XUx.png">
  
    <link rel="alternate" href="/atom.xml" title="Abel&#39;Blog" type="application/atom+xml">
  
  
    <link rel="icon" href="/favicon.png">
  
  
    <link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  
  
<link rel="stylesheet" href="/css/style.css">

<meta name="generator" content="Hexo 5.4.0"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/" id="logo">Abel&#39;Blog</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/">Home</a>
        
          <a class="main-nav-link" href="/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="Search"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="http://example.com"></form>
      </div>
    </div>
  </div>
</header>
      <div class="outer">
        <section id="main"><article id="post-2020/2020-11-14-golang-pprof" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2021/07/28/2020/2020-11-14-golang-pprof/" class="article-date">
  <time datetime="2021-07-28T03:33:01.205Z" itemprop="datePublished">2021-07-28</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      golang-pprof性能分析工具
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <p>编写好了golang服务之后，接着要开始关注服务的CPU，内存使用情况。golang提供了性能剖析工具，记录一些自己搜集到的信息，写下一些实践的情况。在golang中内置了pprof工具，专门来做golang语言的优化。</p>
<ul>
<li><a href="#1%E5%AE%89%E8%A3%85%E7%8E%AF%E5%A2%83">1.安装环境</a></li>
<li><a href="#2%E6%80%A7%E8%83%BD%E7%9B%91%E6%8E%A7%E4%BB%A3%E7%A0%81">2.性能监控代码</a></li>
<li><a href="#3cpu%E6%80%A7%E8%83%BD%E6%9F%A5%E7%9C%8B">3.CPU性能查看</a></li>
<li><a href="#%E5%86%85%E5%AD%98%E6%B3%84%E6%BC%8F">内存泄漏</a></li>
<li><a href="#3%E5%BC%95%E7%94%A8">3.引用</a></li>
</ul>
<h1 id="1-安装环境"><a href="#1-安装环境" class="headerlink" title="1.安装环境"></a>1.安装环境</h1><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">go get -u github.com/google/pprof</span><br></pre></td></tr></table></figure>

<p>需要去下载一个绘图的库。</p>
<p><a target="_blank" rel="noopener" href="http://www.graphviz.org/">Grahpviz</a></p>
<p>.gv文件就是dot工具能读取，并且画图的文件格式。</p>
<p>也推荐使用<a target="_blank" rel="noopener" href="https://marketplace.visualstudio.com/items?itemName=tintinweb.graphviz-interactive-preview">Graphviz Interactive-VSCode插件</a></p>
<p><a target="_blank" rel="noopener" href="https://marketplace.visualstudio.com/items?itemName=joaompinto.vscode-graphviz">vscode-graphviz语法</a></p>
<h1 id="2-性能监控代码"><a href="#2-性能监控代码" class="headerlink" title="2.性能监控代码"></a>2.性能监控代码</h1><p>这段代码将会开启一个http的网站，对外提供监控访问的地址。</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> (</span><br><span class="line">	<span class="string">&quot;net/http&quot;</span></span><br><span class="line">    _ <span class="string">&quot;net/http/pprof&quot;</span></span><br><span class="line">)</span><br><span class="line"><span class="keyword">go</span> <span class="function"><span class="keyword">func</span><span class="params">()</span></span> &#123;</span><br><span class="line">    http.ListenAndServe(<span class="string">&quot;:10003&quot;</span>, <span class="literal">nil</span>)</span><br><span class="line">&#125;()</span><br></pre></td></tr></table></figure>
<p><a target="_blank" rel="noopener" href="https://imgchr.com/i/rL0XUx"><img src="https://s3.ax1x.com/2020/12/30/rL0XUx.png" alt="rL0XUx.png"></a></p>
<p>能通过这个网址检查程序的内存分配，CPU占用，mutex，gorouine之类的信息。可以直接通过浏览器访问获取信息。</p>
<ul>
<li>CPU profile：报告程序的 CPU 使用情况，按照一定频率去采集应用程序在 CPU 和寄存器上面的数据</li>
<li>Memory Profile（Heap Profile）：报告程序的内存使用情况</li>
<li>Block Profiling：报告 goroutines 不在运行状态的情况，可以用来分析和查找死锁等性能瓶颈</li>
<li>Goroutine Profiling：报告 goroutines 的使用情况，有哪些 goroutine，它们的调用关系是怎样的</li>
</ul>
<h1 id="3-CPU性能查看"><a href="#3-CPU性能查看" class="headerlink" title="3.CPU性能查看"></a>3.CPU性能查看</h1><p>打开一个命令行输入：</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">PS</span> D:\work\trunk\doc&gt; go tool pprof <span class="literal">-http</span>=<span class="string">&quot;:8081&quot;</span> http://<span class="number">127.0</span>.<span class="number">0.1</span>:<span class="number">10006</span>/debug/pprof/profile?seconds=<span class="number">200</span></span><br><span class="line">Fetching profile over HTTP from http://<span class="number">127.0</span>.<span class="number">0.1</span>:<span class="number">10006</span>/debug/pprof/profile?seconds=<span class="number">200</span></span><br><span class="line">Saved profile <span class="keyword">in</span> C:\Users\xxxxxxxxx\pprof\pprof.samples.cpu.<span class="number">003</span>.pb.gz</span><br><span class="line">Serving web UI on http://localhost:<span class="number">8081</span></span><br></pre></td></tr></table></figure>

<p>在本地将会开启 <strong>-http=”:8081”</strong> 表示在8081网站，将会呈现最后结果。这次监控将会抓取 <a target="_blank" rel="noopener" href="http://127.0.0.1:10006/debug/pprof/profile">http://127.0.0.1:10006/debug/pprof/profile</a> 提供的性能信息。**?seconds=200** 表示持续抓取200sec。到时间之后它们将会存储到本地的一个文件中： Saved profile in C:\Users\xxxxxxxxx\pprof\pprof.samples.cpu.003.pb.gz 。</p>
<p>想知道这个处理的细节，可以直接阅读golang的源码：</p>
<figure class="highlight golang"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// C:\Go\src\net\http\pprof\pprof.go</span></span><br><span class="line"><span class="comment">// Profile responds with the pprof-formatted cpu profile.</span></span><br><span class="line"><span class="comment">// Profiling lasts for duration specified in seconds GET parameter, or for 30 seconds if not specified.</span></span><br><span class="line"><span class="comment">// The package initialization registers it as /debug/pprof/profile.</span></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">Profile</span><span class="params">(w http.ResponseWriter, r *http.Request)</span></span> &#123;</span><br><span class="line">	w.Header().Set(<span class="string">&quot;X-Content-Type-Options&quot;</span>, <span class="string">&quot;nosniff&quot;</span>)</span><br><span class="line">	sec, err := strconv.ParseInt(r.FormValue(<span class="string">&quot;seconds&quot;</span>), <span class="number">10</span>, <span class="number">64</span>)</span><br><span class="line">	<span class="keyword">if</span> sec &lt;= <span class="number">0</span> || err != <span class="literal">nil</span> &#123;</span><br><span class="line">		sec = <span class="number">30</span></span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>它的接口有两个能支持输入seconds</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">&quot;trace&quot;:        &quot;A trace of execution of the current program. You can specify the duration in the seconds GET parameter. After you get the trace file, use the go tool trace command to investigate the trace.&quot;,</span><br><span class="line">&quot;profile&quot;:      &quot;CPU profile. You can specify the duration in the seconds GET parameter. After you get the profile file, use the go tool pprof command to investigate the profile.&quot;,</span><br></pre></td></tr></table></figure>

<p>通过加载文件来调出分析网站：</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">PS</span> D:\work\trunk\doc&gt; go tool pprof <span class="literal">-http</span>=<span class="string">&quot;:8989&quot;</span> C:\Users\xxxxxxxxx\pprof\pprof.samples.cpu.<span class="number">003</span>.pb.gz</span><br><span class="line">Serving web UI on http://localhost:<span class="number">8989</span></span><br></pre></td></tr></table></figure>

<p>也能通过这个命令直接加载之前监控的剖析文件。</p>
<table>
<thead>
<tr>
<th align="left">列名</th>
<th align="left">含义</th>
</tr>
</thead>
<tbody><tr>
<td align="left">flat</td>
<td align="left">函数时间（不包括子函数）。这里不包括函数等待子函数返回。</td>
</tr>
<tr>
<td align="left">flat%</td>
<td align="left">flat / 总采样时间值</td>
</tr>
<tr>
<td align="left">sum%</td>
<td align="left">前面每一行的flat占比总和</td>
</tr>
<tr>
<td align="left">cum</td>
<td align="left">函数总时间（包括子函数）。因此 flat &lt;= cum</td>
</tr>
<tr>
<td align="left">cum%</td>
<td align="left">cum占用总时间的比例</td>
</tr>
</tbody></table>
<p>火焰图方式</p>
<p><a target="_blank" rel="noopener" href="https://imgchr.com/i/rLfQUg"><img src="https://s3.ax1x.com/2020/12/30/rLfQUg.png" alt="rLfQUg.png"></a></p>
<p>调用关系图</p>
<p><a target="_blank" rel="noopener" href="https://imgchr.com/i/rL0OV1"><img src="https://s3.ax1x.com/2020/12/30/rL0OV1.png" alt="rL0OV1.png"></a></p>
<p>图片说明：</p>
<h1 id="内存泄漏"><a href="#内存泄漏" class="headerlink" title="内存泄漏"></a>内存泄漏</h1><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">curl localhost:8000/debug/pprof/heap &gt; heap.base</span><br><span class="line">curl localhost:8000/debug/pprof/heap &gt; heap.current</span><br><span class="line">go tool pprof -http=:8080 -base heap.base heap.current</span><br><span class="line"></span><br><span class="line">    -diff_base <span class="built_in">source</span>     Source of base profile <span class="keyword">for</span> comparison</span><br><span class="line">    -base <span class="built_in">source</span>          Source of base profile <span class="keyword">for</span> profile subtraction</span><br></pre></td></tr></table></figure>

<h1 id="3-引用"><a href="#3-引用" class="headerlink" title="3.引用"></a>3.引用</h1><ul>
<li>[1] <a target="_blank" rel="noopener" href="https://github.com/uber-archive/go-torch">火焰图工具网站</a></li>
<li>[2] <a target="_blank" rel="noopener" href="https://zhuanlan.zhihu.com/p/51559344">Golang性能测试工具PProf应用详解</a></li>
<li>[3] <a target="_blank" rel="noopener" href="https://zhuanlan.zhihu.com/p/91241270">深度解密go语言之pprof</a></li>
<li>[4] <a target="_blank" rel="noopener" href="http://graphviz.org/">graphviz官网</a></li>
<li>[5] <a target="_blank" rel="noopener" href="https://www.bookstack.cn/read/eddycjy-go/tools-go-tool-pprof.md">Go 大杀器之性能剖析 PProf</a></li>
<li>[6] <a target="_blank" rel="noopener" href="https://sourceforge.net/projects/qcachegrindwin/">qcachegrindwin下载地址</a></li>
<li>[7] <a target="_blank" rel="noopener" href="https://studygolang.com/articles/29812?fr=sidebar">golang内存泄漏的排查记录</a></li>
</ul>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://example.com/2021/07/28/2020/2020-11-14-golang-pprof/" data-id="ckrn3an72000p20xw5tqq2xdt" class="article-share-link">Share</a>
      
      
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/golang/" rel="tag">golang</a></li></ul>

    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/2021/07/28/2020/2020-11-16-supervisor/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          Supervisor使用详解
        
      </div>
    </a>
  
  
    <a href="/2021/07/28/2020/2020-11-12-bash/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">Linux-bash</div>
    </a>
  
</nav>

  
</article>

</section>
        
          <aside id="sidebar">
  
    

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tags</h3>
    <div class="widget">
      <ul class="tag-list" itemprop="keywords"><li class="tag-list-item"><a class="tag-list-link" href="/tags/3D/" rel="tag">3D</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/AI/" rel="tag">AI</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/C/" rel="tag">C++</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Game/" rel="tag">Game</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Go/" rel="tag">Go</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Linux/" rel="tag">Linux</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Math/" rel="tag">Math</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Server/" rel="tag">Server</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Unreal-Engine4/" rel="tag">Unreal Engine4</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/git/" rel="tag">git</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/golang/" rel="tag">golang</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/guitar/" rel="tag">guitar</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/node-js/" rel="tag">node.js</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/protobuf/" rel="tag">protobuf</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/python/" rel="tag">python</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/recast/" rel="tag">recast</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/redis/" rel="tag">redis</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/type/" rel="tag">type</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/vscode/" rel="tag">vscode</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E8%BF%90%E7%BB%B4/" rel="tag">运维</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tag Cloud</h3>
    <div class="widget tagcloud">
      <a href="/tags/3D/" style="font-size: 10px;">3D</a> <a href="/tags/AI/" style="font-size: 11.67px;">AI</a> <a href="/tags/C/" style="font-size: 15px;">C++</a> <a href="/tags/Game/" style="font-size: 18.33px;">Game</a> <a href="/tags/Go/" style="font-size: 13.33px;">Go</a> <a href="/tags/Linux/" style="font-size: 13.33px;">Linux</a> <a href="/tags/Math/" style="font-size: 11.67px;">Math</a> <a href="/tags/Server/" style="font-size: 16.67px;">Server</a> <a href="/tags/Unreal-Engine4/" style="font-size: 10px;">Unreal Engine4</a> <a href="/tags/git/" style="font-size: 10px;">git</a> <a href="/tags/golang/" style="font-size: 20px;">golang</a> <a href="/tags/guitar/" style="font-size: 10px;">guitar</a> <a href="/tags/node-js/" style="font-size: 10px;">node.js</a> <a href="/tags/protobuf/" style="font-size: 10px;">protobuf</a> <a href="/tags/python/" style="font-size: 10px;">python</a> <a href="/tags/recast/" style="font-size: 11.67px;">recast</a> <a href="/tags/redis/" style="font-size: 10px;">redis</a> <a href="/tags/type/" style="font-size: 10px;">type</a> <a href="/tags/vscode/" style="font-size: 10px;">vscode</a> <a href="/tags/%E8%BF%90%E7%BB%B4/" style="font-size: 16.67px;">运维</a>
    </div>
  </div>

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/07/">July 2021</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Recent Posts</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/2021/07/28/2021/2021-07-19-etcd-learn/">go-etcd阅读笔记</a>
          </li>
        
          <li>
            <a href="/2021/07/28/2021/2021-06-22-go-ethereum-learn/">go-ethereum阅读笔记</a>
          </li>
        
          <li>
            <a href="/2021/07/28/2021/2021-06-21-server%E6%B5%8B%E8%AF%95%E5%B7%A5%E5%85%B7/">server测试工具</a>
          </li>
        
          <li>
            <a href="/2021/07/28/2021/2021-06-09-pprof-doc/">go-pprof</a>
          </li>
        
          <li>
            <a href="/2021/07/28/2021/2021-04-19-c++-metaprogramming/">c++metaprogramming</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      &copy; 2021 Abel Sean<br>
      Powered by <a href="http://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>
    </div>
    <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>


  
<link rel="stylesheet" href="/fancybox/jquery.fancybox.css">

  
<script src="/fancybox/jquery.fancybox.pack.js"></script>




<script src="/js/script.js"></script>




  </div>
</body>
</html>